//编辑限时统一折扣活动

//js去掉空格
function trim(str){
    return  (str+"").replace(/^\s+|\s+$/g,"");
}
function cancle(){
	var index = parent.layer.getFrameIndex(window.name);
	parent.layer.close(index);
}
function toAddProduct(){
	var url=path+"/product_sale/to_add_product";
    var index = layer.open({
        type: 2,
        title: "添加到活动商品列表",
        scrollbar: false,
        content: url,
        area: ['1000px','600px']
    });
}
//添加选中的商品ID
function addProduct(productId){
	var productIds = document.getElementById("saleProducIds").value;
	var productIdArr = 	productIds.split(",");
	var exist = false;
	for(var i=0;i<productIdArr.length;i++){
		if(productIdArr[i] == productId){
			exist = true;
			break;
		}
	}
	if(!exist){
		if(productIds.length == 0){
			productIds = productIds + productId  + ",";
		}else{
			productIds = productIds + productId + ",";
		}
	}
	document.getElementById("saleProducIds").value = productIds;
	
	$("#saleProductLength").val(productIdArr.length);
}

//	删除商品ID
function deleteProduct(productId){
	var productIds = document.getElementById("saleProducIds").value;
	var productIdArr = 	productIds.split(",");
	var result = "";
	for(var i=0;i<productIdArr.length;i++){
		if(productIdArr[i] != productId){
			if(productIdArr.length - 1 == i){
				result = result + productIdArr[i];
			}else{
				result = result + productIdArr[i] + ",";
			}
		}
	}
	document.getElementById("saleProducIds").value = result;
	
	$("#sale"+productId).remove();
	var saleLength = $("#saleProductLength").val();
	saleLength --;
	
	$("#saleProductLength").val(saleLength);
	$("#product_id").html("您已选择"+saleLength+"款商品");
}
$(function(){
	$("body").on("click",".del-c",function(){
		var productId = $(this).attr("id");
		deleteProduct(productId);
	});
});
function sub_form(){
	   var saleName = $("input[name='name']").val();
	   saleName =trim(saleName);
	   if(saleName == null || saleName == ""){
		layer.alert("请填写活动名称");
		   return false;
	   }
	   var startDate = $("input[name='startDate']").val();
	   startDate = trim(startDate);
	   if(startDate == null || startDate == ""){
		layer.alert("请填写开始时间");
		   return false;
	   }
	   var endDate = $("input[name='endDate']").val();
	   endDate = trim(endDate);
	   if(endDate == null || endDate == ""){
		layer.alert("请填写结束时间");
		   return false;
	   }
	   var sale = $("input[name='sale']").val();
	    sale = trim(sale);
	    if(sale<=0||sale>1||sale==1){
	   layer.alert("折扣必须在0~1之间");
	       return false;
	    }
	   if(startDate>endDate||startDate==endDate){
	   layer.alert("结束时间必须大于开始时间");
	      return false; 
	   }
	   var length=$("#saleProductLength").val();
	    var ids= document.getElementById("saleProducIds").value;
	   if(trim(ids)==""||trim(ids)==","||length<=0){
	    layer.alert("请选择添加的商品");
	       return false;
	   }
	$("#save_form").ajaxSubmit({
		success:function(data){
			var resultArr = data.split(",");
	   		if(resultArr[0] == "success"){
				layer.msg(resultArr[1],{icon:1,time:1000},function(){
					var index = parent.layer.getFrameIndex(window.name);
					parent.location.reload();
					parent.layer.close(index);
				});
	   		}else if(resultArr[0] == "fail"){
	   			layer.alert(resultArr[1]);
	   		}else{
	   			layer.alert("操作失败!");
	   		}
		},
		error:function(data){
			layer.alert("操作失败!");
		}
	});
}